//
//  UIButton+XBAdd.m
//  XBCategoriesDemo
//
//  Created by kevin007 on 2021/4/12.
//

#import "UIButton+XBAdd.h"
#import "NSObject+XBAdd.h"

@implementation UIButton (XBAdd)
- (void)setEnlargeTouchEdgeInsets:(UIEdgeInsets)enlargeTouchEdgeInsets {
    NSString *edgeStr = NSStringFromUIEdgeInsets(enlargeTouchEdgeInsets);
    [self setAssociateValue:edgeStr withKey:@selector(enlargeTouchEdgeInsets)];
}

- (UIEdgeInsets)enlargeTouchEdgeInsets {
    NSString *edgeStr = [self getAssociatedValueForKey:_cmd];
    return edgeStr.length?UIEdgeInsetsFromString(edgeStr):UIEdgeInsetsZero;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIEdgeInsets changeInsets = self.enlargeTouchEdgeInsets;
    CGFloat x = self.bounds.origin.x + changeInsets.left;
    CGFloat y = self.bounds.origin.y + changeInsets.top;
    CGFloat width = self.bounds.size.width - changeInsets.left - changeInsets.right;
    CGFloat height = self.bounds.size.height - changeInsets.top - changeInsets.bottom;
    CGRect rect = CGRectMake(x, y, width, height);
    
    if (CGRectEqualToRect(rect, self.bounds)) {
        return [super hitTest:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) ? self : nil;
}

#pragma mark - -----------------Button Image位置设置-------------------
/// 交换按钮图片文字的文字 间隙6.0f, 如果使用Masonry等相对布局，请在- (void)layoutSubviews中调用
/// @param style 图片位置
- (void)setButtonImagePositionWithStyle:(XBButtonImagePositionStyle)style {
    [self setButtonImagePositionWithStyle:style space:6.0f];
}

/// 交换按钮图片文字的文字 设置间隙, 如果使用Masonry等相对布局，请在- (void)layoutSubviews中调用
/// @param style 图片位置
/// @param space 间隙
- (void)setButtonImagePositionWithStyle:(XBButtonImagePositionStyle)style space:(CGFloat)space {
    if (!self.currentImage || self.titleLabel.text.length == 0 || self.frame.size.height == 0 || self.frame.size.width == 0) return;
    
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
    
    UIEdgeInsets imageInset = UIEdgeInsetsZero;
    UIEdgeInsets titleInset = UIEdgeInsetsZero;
    CGFloat s = space * 0.5;
    if (style ==XBButtonImagePositionStyleTop) {
        imageInset = UIEdgeInsetsMake(-titleSize.height - s, 0, 0, -titleSize.width);
        titleInset = UIEdgeInsetsMake(0, -imageSize.width, -imageSize.height - s, 0);
    } else if (style ==XBButtonImagePositionStyleRight) {
        imageInset = UIEdgeInsetsMake(0, titleSize.width + s, 0, -titleSize.width - s);
        titleInset = UIEdgeInsetsMake(0, -imageSize.width - s, 0, imageSize.width + s);
    } else if (style ==XBButtonImagePositionStyleBottom) {
        imageInset = UIEdgeInsetsMake(0, 0, -titleSize.height - s, -titleSize.width);
        titleInset = UIEdgeInsetsMake(-imageSize.height - s, -imageSize.width, 0, 0);
    } else {
        imageInset = UIEdgeInsetsMake(0, -s, 0, s);
        titleInset = UIEdgeInsetsMake(0, s, 0, -s);
    }
    
    self.titleEdgeInsets = titleInset;
    self.imageEdgeInsets = imageInset;
}
@end
